<?php
//模块名_控制名
class Customer_CustomerController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "customer/customer/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$total = Customer_Service_Customer::getByCond(array(),'count(*)','customer_id',0,0);
		$this->view->total = $total;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$customer_id = $this->_request->getParam('customer_id','');
    	$customer_status = $this->_request->getParam('status','');
    	$cond =array();
    	if(isset($customer_status)  && $customer_status !==''){
    		$cond['customer_status'] = $customer_status;
    	}		
    	if(!empty($customer_id)){    		
    		$cond['customer_id'] = $customer_id;
    	}

    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize');
		
    	$orderby = 'customer_id';
		$result = Customer_Service_Customer::getByCond($cond,'*',$orderby, $pageSize, ($page - 1)*$pageSize);		
		$total = Customer_Service_Customer::getByCond($cond,'count(*)',$orderby, 0, 0);
		$data=array('ask'=>0);
		if($total>0){			
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
    }
    
    public function editAction(){
    	$customer_id = $this->_request->getParam('customer_id','');
    	$customer_name = $this->_request->getParam('customer_name','');
    	$customer_firstname = $this->_request->getParam('customer_firstname','');
    	$customer_lastname = $this->_request->getParam('customer_lastname','');
    	$customer_email = $this->_request->getParam('customer_email','');
    	
    	$customer_telephone = $this->_request->getParam('customer_telephone','');
    	$customer_fax = $this->_request->getParam('customer_fax','');
    	$customer_status = $this->_request->getParam('customer_status','');
    	$customer_company_name = $this->_request->getParam('customer_company_name','');
    	$languages_id = $this->_request->getParam('languages_id','');
    	
    	$row = array();
    	$customer_name && $row['customer_name'] = $customer_name;
    	$customer_firstname && $row['customer_firstname'] = $customer_firstname;
    	$customer_lastname && $row['customer_lastname'] = $customer_lastname;
    	$customer_email && $row['customer_email'] = $customer_email;
    	
    	$customer_telephone && $row['customer_telephone'] = $customer_telephone;    	
    	$customer_fax && $row['customer_fax'] = $customer_fax;
    	$customer_company_name && $row['customer_company_name'] = $customer_company_name;
    	$languages_id && $row['languages_id'] = $languages_id;
    	
    	if($customer_status || $customer_status ==='0'){
    		$row['customer_status'] = $customer_status;
    	}
		
		if($customer_id){
			$result = Customer_Service_Customer::update($row,$customer_id);
		}
		
		if($result){
			$msg = array('ask'=>1);
    	}else{
    		$msg = array('ask'=>0);
    	}
		echo json_encode($msg);	
    }
    
    public function deleteAction(){
    	$customer_id = $this->_request->getParam('id','');
    	if($customer_id){
    		$result = Customer_Service_Customer::delete($customer_id);
    		if($result){
    			$msg = array('ask'=>1);
    		}else{
    			$msg = array('ask'=>0);
    		}
    		echo json_encode($msg);
    	}

    }
    
    public function singleAction(){
    	$customer_id = $this->_request->getParam('id','');
    	if($customer_id){
    		$result = Customer_Service_Customer::getByCustomerId($customer_id);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
				die(Zend_Json::encode($data));
			}
    	}
    }
	
	/**
	*按客户ID查询套餐详细
	*@param customer_id
	*/
	public function getplanAction(){
		$obj = new Plan_Service_Plan();
		$upgradestatus=array(
			'0'=>'正常',
			'1'=>'升级',
			'-1'=>'降级',
		);
		$customer_id = $this->_request->getParam('customer_id','');
		$PlanRows=$obj->getCurrentPlan($customer_id);
		if($customer_id==''||empty($PlanRows)){
			die('操作失败,此客户未使用套餐!');
		}
		$PlanUsageRows=$obj->getCurrentPlanUsage($customer_id);
		
		//获取以前使用套餐列表
		$condition=array(
			'customer_id'=>$customer_id,
			'active'=>'0',//非当前套餐
		);
		$planusagelist=$obj->getByPlanUsageCondition($condition,'*',array('pu_id desc'),0,0);
		foreach($planusagelist as $key => $value){
			$PlanRow=$obj->getByPlanId($value['plan_id']);
			//$planusagelist[$key]['billing_date']=$PlanRow['billing_date'];   //扣款日
			$planusagelist[$key]['total']=$PlanRow['total'];
			$planusagelist[$key]['upgrade']=$upgradestatus[$PlanRow['upgrade']];
		}
		$this->view->planusagelist = $planusagelist;
		
		$PlanRows['upgrade']=$upgradestatus[$PlanRows['upgrade']];//升级状态
		$this->view->PlanUsageRows = $PlanUsageRows;
		$this->view->PlanRows = $PlanRows;
		$this->view->customer_id = $customer_id;
		$this->view->pu_id = $PlanUsageRows['pu_id'];					//套餐用量ID

		echo Cff::renderTpl($this->tplDirectory."plan.tpl","layoutheader");
    }
	
	/**
	*按套餐用量ID及客户ID查询套餐明细
	*@param pu_id,customer_id
	*/
	public function getplanusagedetailAction(){
		$pu_id = $this->_request->getParam('pu_id','');
		$customer_id = $this->_request->getParam('customer_id','');
		$this->view->pu_id = $pu_id;									//套餐用量ID
		$this->view->customer_id = $customer_id;
		if($pu_id==''){
			die('操作失败!');
		}
		echo Cff::renderTpl($this->tplDirectory."plan-usage-detail.tpl","layoutheader");
    }
	
	/**
	*按客户ID及套餐用量ID查询套餐使用详细
	*@param customer_id pu_id
	*return json
	*/
	public function planusagedetailAction(){
		$obj = new Plan_Service_Plan();
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$customer_id = $this->_request->getParam('customer_id','');  	//客户ID
		$pu_id = $this->_request->getParam('pu_id','');					//套餐用量ID
		$intpage = ($intpage<1)?1:$intpage;
		if($customer_id=='' || $pu_id==''){
			die(Zend_Json::encode(array('ask'=>0,'error'=>'操作失败!')));
		}
		$condition=array(
			'customer_id'=>$customer_id,
			'pu_id'=>$pu_id,
		);
		$count=$obj->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$result=$obj->getByCondition($condition,'*',array('pud_id desc'),$pageSize,$intpage);
			$data=array(
				'count'=>$count,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
	
}